package ${entity.packageName}.logic.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import ${entity.packageName}.bean.${entity.entityName}Bean;
import ${entity.packageName}.po.${entity.entityName}SearchPo;
import org.apache.ibatis.annotations.*;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.List;

/**
 * ${entity.entityLabel}数据库处理
 */
@Mapper
public interface ${entity.entityName}Mapper extends BaseMapper<${entity.entityName}Bean> {

    String ${entity.entityNameFullUpperCase}_PAGEABLE = "" +
            "<if test=\"!pageable.sort.toString().equals('UNSORTED')\">" +
            "    order by" +
            "    <foreach item=\"item\" index=\"index\" collection=\"pageable.sort.orders\" separator=\",\">" +
            "        ${r't.${'}item.property} ${r'${'}item.direction}" +
            "    </foreach>" +
            "</if>" +
            "limit ${r'${'}pageable.page * pageable.size},${r'${'}pageable.size} ";

    @Results(id = "${entity.entityName}Bean", value = {

            @Result(column = "${entity.entityNameFirstLower}_key", property = "${entity.entityNameFirstLower}Key"),
            <#list entity.fieldList as f>@Result(column = "${f.nameUnderline}", property = "${f.name}"),
            </#list>
    })
    @Select("<script>" +
            "select " +
            "   t.* " +
            "from ${config.tablePrefix}${entity.dbTableName} as t " +
            "where 1=1 " +
            <#list entity.fieldList as f><#if f.searchable >"<if test = \"searchPo.${f.name} != null<#if f.type == 'String'> and ${f.name} != ''</#if>\">" +
            "   and <#if f.supportFuzzySearch >t.${f.nameUnderline} like concat('%', ${r'#{'}searchPo.${f.name}},'%')<#else/>t.${f.nameUnderline} = ${r'#{'}searchPo.${f.name}}</#if> " +
            "</if>" +
            </#if>
            </#list>"<if test = \"searchPo.${entity.entityNameFirstLower}Keys != null and searchPo.${entity.entityNameFirstLower}Keys.size() &gt; 0\">" +
            "   and t.${entity.entityNameFirstLower}_key in " +
            "   <foreach collection=\"searchPo.${entity.entityNameFirstLower}Keys\" item=\"item\" index=\"index\" separator=\",\" open=\"(\" close=\")\">" +
            "       ${r'#{'}item}" +
            "   </foreach>" +
            "</if>" +
//            "group by t.${entity.entityNameFirstLower}_key " +
            ${entity.entityNameFullUpperCase}_PAGEABLE +
            "</script>")

    /**
     * 查询${entity.entityLabel}
     */
    List<${entity.entityName}Bean> search${entity.entityName}(@Param("searchPo") ${entity.entityName}SearchPo searchPo,
                                @Param("pageable") Pageable pageable);

    /**
     * 查询${entity.entityLabel}数量
     */
    @Select("<script>" +
            "select " +
            "COUNT(distinct t.${entity.entityNameFirstLower}_key)" +
            "from gs_meeting_${entity.entityNameFirstLower} as t " +
            "where 1=1 " +
            <#list entity.fieldList as f><#if f.searchable >"<if test = \"searchPo.${f.name} != null<#if f.type == 'String'> and ${f.name} != ''</#if>\">" +
            "   and <#if f.supportFuzzySearch >t.${f.nameUnderline} like concat('%', ${r'#{'}searchPo.${f.name}},'%')<#else/>t.${f.nameUnderline} = ${r'#{'}searchPo.${f.name}}</#if> " +
            "</if>" +
            </#if>
            </#list>"<if test = \"searchPo.${entity.entityNameFirstLower}Keys != null and searchPo.${entity.entityNameFirstLower}Keys.size() &gt; 0\">" +
            "   and t.${entity.entityNameFirstLower}_key in " +
            "   <foreach collection=\"searchPo.${entity.entityNameFirstLower}Keys\" item=\"item\" index=\"index\" separator=\",\" open=\"(\" close=\")\">" +
            "       ${r'#{'}item}" +
            "   </foreach>" +
            "</if>" +
            "</script>")
    int count${entity.entityName}(@Param("searchPo") ${entity.entityName}SearchPo searchPo);
}
